const { Tab } = require('../../assets/libs/zanui/index');

var app = getApp();
Page(Object.assign({}, Tab, {
  data: {
    userInfo: '',
		cdnUrl: '',
		/* 自定义导航栏参数 */
		loading: false,
		color: '#FFFFFF',
		background: '#02B3BA',
		show: true,
		animated: false,
		back: false,
		title: "蓝之鲸回收",
    cardCur: 0,
    swiperList: [],
		/* 自定义tabbar */
		list: [
			{
			    "pagePath": "/page/index/index",
			    "iconPath": "/assets/images/tabbar/index.png",
			    "selectedIconPath": "/assets/images/tabbar/index-hl.png",
			    "text": "首页"
			},
			{
			    "pagePath": "/page/discover/index",
			    "iconPath": "/assets/images/tabbar/addon.png",
			    "selectedIconPath": "/assets/images/tabbar/addon-hl.png",
			    "text": "发现"
			},
			{
			    "pagePath": "/page/my/index",
			    "iconPath": "/assets/images/tabbar/my.png",
			    "selectedIconPath": "/assets/images/tabbar/my-hl.png",
			    "text": "我的"
			}
		],
		currentTab: 0,
	  images:[],
  },
  tabChange(e) {
  	wx.reLaunch({
  	  url: e.detail.item.pagePath
  	})
  },
  //前往物品回收页面
  toRecovery(e){

   if(e.currentTarget.dataset.type=='5'){
    wx.navigateTo({
  	  url: "/page/booking_order/index"
    })  
   }else{
    wx.navigateTo({
  	  url: "../recoveryItem/recoveryItem?type="+e.currentTarget.dataset.type
    })
   }
   
  },
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  onLoad: function () {
    var that = this;
    this.towerSwiper('swiperList');
    this.getBanner();
		that.setData({
			cdnUrl: app.cdnUrl,
      userInfo: app.globalData.userInfo
    });
    console.log();
		wx.getUserProfile({
		  lang:'zh',
		  desc:'授权用户信息',
		  success:function(res){
				console.log(res);
				app.login(res.userInfo,function () {
					that.setData({ userInfo: app.globalData.userInfo });
				});
		  },
    });
    
    wx.getLocation({
      type: 'gcj02',
      isHighAccuracy:true,
      highAccuracyExpireTime:3000,
      success (res) {
        console.log(res);
        const latitude = res.latitude
        const longitude = res.longitude
        that.getCityName(latitude,longitude);
      }
     })
  },
  getBanner(){
    var that = this;
    app.request('/api/index/banner', {
    }, function (data, ret) {
        console.log("banner",data);
        that.setData({
          swiperList:data
      })
    }, function (data, ret) {
      
    });
  },
  getCityName(lat,lng){
    var that = this;
    app.request('/api/index/cityName', {
      latitude:lat,
      longitude:lng
    }, function (data, ret) {
        console.log("cityName",data);
        wx.setStorageSync('userLocation', data)
        that.setData({
          userLocation:data
      })
    }, function (data, ret) {
      
    });
  },
	onShow: function (){
		var that = this;
		if (!app.globalData.userInfo) {
		  app.check(that);
		}
		that.setData({
			images: [
				that.data.cdnUrl+"/uploads/20210511/cf8c74bcb47b98a4ba2e39a6fe910bbe.png",
				that.data.cdnUrl+"/uploads/20210511/d75209f44050933e7f7f8a2284b38f2a.png",
				that.data.cdnUrl+"/uploads/20210511/6a254cd52ee17ad19e1fccffd0b2752f.png",
				that.data.cdnUrl+"/uploads/20210511/9920f538c2b0187685ca88744f69665d.png",
				that.data.cdnUrl+"/uploads/20210511/9920f538c2b0187685ca88744f69665d.png",
			]
		})
	},
  onShareAppMessage: function () {
    return {
      title: '',
      desc: '',
      path: '/page/index/index'
    };
  },
  /* 拨打客服电话*/
  makePhone: function(){
    wx.makePhoneCall({
      phoneNumber: app.globalData.config.customer_service_mobile
    })
  },
  //跳转小程序
  toRider(){
    wx.navigateToMiniProgram({
      appId: 'wxdd3d1a71f2c84d62',
      path: 'page/index/index',
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  }
}));
